%This script produces the adjustment vectors for the share matrices used to
%compute the IVs in simulation
%Corresponding author: Rodrigo Adao
%Date: 09/11/2024
%Input: model_data_fgkk.mat

%% Preliminaries

clear;
close all;

%Define vector of large varieties in our sample
trade_target = 0.9; %baseline has 0.9

%% Compute matrix of shares for baseline calibration
clearvars -except trade_target date_version M

%set local paths
function_path = "..\Functions\";
addpath(function_path,'-frozen');
set_local_paths;

%Import data and compute researcher predictions
simulation_preliminary_steps;

% Compute matrix of shares

%Compute MC times share_sharep
tic
    share_mat = shareMOD_dW;
    share_mat_T = share_mat';
    
    share_sharep = zeros(Nobs, Nobs);
    MCa_share_sharep = zeros(Nobs,Nobs);
    MC_share_sharep = zeros(Nobs,Nobs);
    
    MC_term3 = eye(Nobs) ;

   parfor s=1:Nobs 
        auxT = share_mat_T(:,s);
        aux0 = share_mat*auxT;
        share_sharep(:,s) = aux0;
   
        aux1 = MCa_term1(:,s);
        MC_term2=Ca*aux1;
        aux2= MC_term3(:,s)  - MC_term2;
        MCa_share_sharep(:,s) = aux2.*aux0;        

        aux1 = MC_term1(:,s);
        MC_term2=C*aux1;
        aux2= MC_term3(:,s)  - MC_term2;
        MC_share_sharep(:,s) = aux2.*aux0;        
   end
toc


%Compute welfare adjustment vector
tic    
    omega_share = ww_dW.*shareMOD_dWbar;   

    adjGE_wNC  = omega_share./diag( share_sharep );    
    adjGE_wMCa = MCa_share_sharep\omega_share;
    adjGE_wMC  = MC_share_sharep\omega_share;

    unwsign_share = ww_dWsign_mat*(shareMOD_dWbar);
    adjGE_sMC = MC_share_sharep\unwsign_share;
toc

%output to save
save(save_share_path + "simulation_shares_t" + trade_target + ".mat", 'adjGE_wNC', 'adjGE_wMCa', 'adjGE_wMC', 'adjGE_sMC', 'trade_target')


%% Compute matrix of shares for alternative calibration used in simulations with estimation (Figure 5)
clearvars -except trade_target M
set_local_paths;

trade_target = trade_target - 0.05; %adjust trade target to reduce computation time

%Import data and compute researcher predictions
sigma_alt = 4;
simulation_preliminary_steps;

% Compute matrix of shares

    %Compute MC times share_sharep
    tic 
        share_mat = shareMOD_dW;
        share_mat_T = share_mat';
        
        share_sharep = zeros(Nobs, Nobs);
        MCa_share_sharep = zeros(Nobs,Nobs);
        MC_share_sharep = zeros(Nobs,Nobs);
        
        MC_term3 = eye(Nobs) ;
    
       parfor s=1:Nobs 
            auxT = share_mat_T(:,s);
            aux0 = share_mat*auxT;
            share_sharep(:,s) = aux0;
       
            aux1 = MCa_term1(:,s);
            MC_term2=Ca*aux1;
            aux2= MC_term3(:,s)  - MC_term2;
            MCa_share_sharep(:,s) = aux2.*aux0;        
    
            aux1 = MC_term1(:,s);
            MC_term2=C*aux1;
            aux2= MC_term3(:,s)  - MC_term2;
            MC_share_sharep(:,s) = aux2.*aux0;        
       end
    toc


%Compute welfare adjustment vector
    tic        
        omega_share = ww_dW.*shareMOD_dWbar;   
    
        adjGE_wNC  = omega_share./diag( share_sharep );    
        adjGE_wMCa = MCa_share_sharep\omega_share;
        adjGE_wMC  = MC_share_sharep\omega_share;
    
        unwsign_share = ww_dWsign_mat*(shareMOD_dWbar);
        adjGE_sMC = MC_share_sharep\unwsign_share;
    toc

%output to save
save(save_share_path + "simulation_shares_t" + trade_target + ".mat", 'adjGE_wNC', 'adjGE_wMCa', 'adjGE_wMC', 'adjGE_sMC', 'trade_target')
